package com.gemme.gemme.setting;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.login.Constant;
import com.gemme.gemme.login.LoginActivity;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.view.MyButton;
import com.gemme.gemme.view.Topview;
import com.gemme.gemme.view.TvEditText;
import com.gemme.gemme.view.TvRightEditText;
import com.jaeger.library.StatusBarUtil;

import org.json.JSONObject;

import java.math.BigDecimal;
import java.util.HashMap;

public class AdviceActivity extends BaseActivity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advice);
        initView();
        initTop();
    }
    MyButton bt_submit;
    private void initView() {
        TextView tv_content= findViewById(R.id.tv_content);

        bt_submit = findViewById(R.id.bt_submit);
        bt_submit.setSatate(false);
        bt_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                toServer();
            }
        });
    }

    private void toServer() {
        String num_text = editV.getText() + "";
        if(TextUtils.isEmpty(num_text)){
            return;
        }


        String url = DataManager.BaseUrl + "user/pfeedback/savefeedback?" +
                "content" + num_text;
        HashMap<String, String> headHashMap = new HashMap<>();
        headHashMap.put(DataManager.AuthorizationKey,DataManager.getToken(this));
        DataManager.toServer2(mContext, url, DataManager.POST_TYPE, "", headHashMap, new DataManager.GeResponse(){

            @Override
            public void onResponse(String response) {

                try {
                    int code = (new JSONObject(response)).getInt("code");
                    if(code == 0){
                        Toast.makeText(mContext, "意见反馈提交成功", Toast.LENGTH_SHORT).show();
                    }else{
                        Toast.makeText(mContext, "意见反馈提交失败", Toast.LENGTH_SHORT).show();
                    }
                    finish();
                }catch (Exception e){

                }
            }
        });
    }

    EditText editV;
    TextView tv_num;
    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("意见反馈");
        TvRightEditText tv_tel = findViewById(R.id.tv_tel);
        tv_tel.setLeftText("联系方式").setRightHintText("请填写您的手机号或邮箱等");


        editV = findViewById(R.id.editV);

        tv_num = findViewById(R.id.tv_num);
        initListener();
    }

    private void initListener() {
        editV.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

               /* if(length >= 240){
                    editV.setEnabled(false);
                }else{
                    editV.setEnabled(true);
                }*/
               setState();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    private void setState() {
        String num_text = editV.getText() + "";
        int length = num_text.length();
        tv_num.setText(length + "/240");
        if(length >= 10){
            bt_submit.setSatate(true);
        }else{
            bt_submit.setSatate(false);
        }

    }


}